﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using intelliEssay.Core.DocumentStructure;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml.Packaging;
using intelliEssay.Core;
using intelliEssayDocumentSignatureChecker;

namespace intelliEssay.Checkers.DocumentSignatureChecker
{
    class DocumentSignatureChecker : IEssayChecker
    {
        public void CheckSignature(Task task)
        {
            WordprocessingDocument doc = task.TaskObjectRoot;
            DocumentStructure ds = task.documentStructure;
            int titleidx = ds.IsMultiMajor ? DocumentStructure.GetItemMappedID("中文题目：",true) : DocumentStructure.GetItemMappedID("中文题目：", false);
            Paragraph titleloc = ds.IsMultiMajor ? ds.CoverMultiMajorInfoList[titleidx] : ds.CoverSingleMajorInfoList[titleidx];
            string title = titleloc.InnerText;
            title = title.Replace(" ", "").Replace("_", "");
            try
            {
                title = title.Substring(5, title.Length - 5);

                if (title != doc.PackageProperties.Title.Replace(" ", ""))
                {
                    task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Signature, "文档题目签名与文档题目签名不符", titleloc, task, true));
                }
            }
            catch
            {
                task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Signature, "文档题目签名与文档题目签名不符", titleloc, task, true));
            }
            int authoridx = ds.IsMultiMajor ? DocumentStructure.GetItemMappedID("姓名：", true) : DocumentStructure.GetItemMappedID("姓名：", false);
            Paragraph authorloc = ds.IsMultiMajor ? ds.CoverMultiMajorInfoList[authoridx] : ds.CoverSingleMajorInfoList[authoridx];
            string author = authorloc.InnerText;
            author = author.Replace(" ", "").Replace("_", "");
            try
            {
                author = author.Substring(3, author.Length - 3);

                if (author != doc.PackageProperties.Creator.Replace(" ", ""))
                {
                    task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Signature, "文档作者签名与姓名不符", authorloc, task, true));

                }
            }
            catch
            {
                task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Signature, "文档作者签名与姓名不符", authorloc, task, true));
            }
        }
        public void Run(Task tsk)
        {
            CheckSignature(tsk);
        }
        public void Cancel(Task tsk)
        {
            throw new NotImplementedException();
        }



        public string GetDescription(string cultureInfo)
        {
            return "Checks the presence of the signature of the document. Verifies author of the document and owner of file are consistent.";
        }

        public string GetName(string cultureInfo)
        {
            return "Document Signature Checker";
        }

    }
}
